home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Essentials / Dynamo 4.2 for GSBug 1.5b10 / rtfp.a < prev   
Encoding:
Text File  |  1990-09-21  |  14.9 KB  |  1,038 lines  |  [TEXT/MPS ]

  1. ****************************************************************
  2. *                            *
  3. * DYNAMO                            *
  4. *                            *
  5. * Apple II 8-bit floating-point runtime library routines.    *
  6. * Copyright (C) 1990 Apple Computer.            *
  7. * Version 4.1                        *
  8. *                            *
  9. * Written by Eric Soldan, Apple II DTS            *
  10. *                            *
  11. ****************************************************************
  12.  
  13.         include    ':dynamo.includes:sys.equ'
  14.         import    intspace, floatspace
  15.  
  16. FACEXP        equ    $9D
  17. FACHO        equ    $9E
  18. FACMOH        equ    $9F
  19. FACMO        equ    $A0
  20. FACLO        equ    $A1
  21. FACSGN        equ    $A2
  22.  
  23. FBUFFR        equ    $100
  24.  
  25. GIVAYF        equ    $E2F2
  26. FSUB        equ    $E7A7
  27. FADD        equ    $E7BE
  28. LOG        equ    $E941
  29. FMULT        equ    $E97F
  30. CONUPK        equ    $E9E3
  31. FDIV        equ    $EA66
  32. MOVFM        equ    $EAF9
  33. MOVMF        equ    $EB2B
  34. SGN        equ    $EB90
  35. ABS        equ    $EBAF
  36. FCOMP        equ    $EBB2
  37. QINT        equ    $EBF2
  38. INT        equ    $EC23
  39. FOUT        equ    $ED34
  40. SQR        equ    $EE8D
  41. FPWRT        equ    $EE97
  42. EXP        equ    $EF09
  43. RND        equ    $EFAE
  44. COS        equ    $EFEA
  45. SIN        equ    $EFF1
  46. TAN        equ    $F03A
  47. ATN        equ    $F09E
  48.  
  49. ******************
  50.  
  51.         export    startUnary
  52. startUnary    proc
  53.         export    startUnary0, startUnary1
  54.         export    startBinary, startBinary0
  55.         export    endUnBin, endUnBin0, endUnBin1, beginfloat, endfloat, endfloat0
  56.  
  57.         ldy    #>floatspace    ;Pack and copy float into variable.
  58.         txa
  59.         clc
  60.         adc    #<floatspace
  61.         bcc    startUnary0
  62.         iny
  63. startUnary0    stx    endUnBin1+1    ;Preserve xreg.
  64.         jsr    beginfloat
  65.         jsr    MOVFM
  66.         lda    #0        ;Make sure this general flag
  67.         sta    $A4        ;is initialized.
  68.         sta    $D8        ;Make sure AppleSoft ONERR is off.
  69.         ldx    endUnBin1+1
  70.         rts
  71.  
  72. startUnary1    jsr    beginfloat
  73.         stx    endUnBin1+1
  74.         rts
  75.  
  76. startBinary    tya
  77.         ldy    #>floatspace    ;Pack and copy float into variable.
  78.         clc
  79.         adc    #<floatspace
  80.         bcc    startBinary0
  81.         iny
  82.  
  83. startBinary0    jsr    beginfloat
  84.         stx    endUnBin1+1
  85.         jsr    MOVFM
  86.         lda    #0        ;Make sure this general flag
  87.         sta    $A4        ;is initialized.
  88.         sta    $D8        ;Make sure AppleSoft ONERR is off.
  89.         ldy    #>floatspace
  90.         lda    endUnBin1+1
  91.         clc
  92.         adc    #<floatspace
  93.         bcc    @b
  94.         iny
  95. @b        rts
  96.  
  97. endUnBin        lda    #<floatspace
  98.         clc
  99.         adc    endUnBin1+1
  100.         tax
  101.         lda    #>floatspace
  102.         adc    #0
  103.         tay
  104.         jsr    MOVMF
  105.  
  106. endUnBin0    jsr    endfloat
  107. endUnBin1    ldx    #0        ;Modified.
  108.         clc
  109.         rts
  110.  
  111. beginfloat    sta    @keepa+1
  112.         stx    @keepx+1
  113.         tsx
  114.         stx    keepstkptr+1
  115.         ldx    #0
  116. @a        lda    $0,x
  117.         sta    keepzp,x
  118.         lda    $100,x
  119.         sta    keepstk,x
  120.         inx
  121.         bne    @a
  122.         lda    cswl
  123.         sta    keepcswl+1
  124.         lda    cswh
  125.         sta    keepcswh+1
  126.         lda    #<errhook
  127.         sta    cswl
  128.         lda    #>errhook
  129.         sta    cswh
  130. @keepa        lda    #0        ;Modified.
  131. @keepx        ldx    #0        ;Modified.
  132.         rts
  133. keepzp        ds.b    256
  134. keepstk        ds.b    256
  135.  
  136. endfloat        ldy    #0        ;Restore 0-page and unhook error trap.
  137. @a        lda    keepzp,y
  138.         sta    |$0,y
  139.         iny
  140.         bne    @a
  141. endfloat0
  142. keepcswl        lda    #0        ;Modified.
  143.         sta    cswl
  144. keepcswh        lda    #0
  145.         sta    cswh
  146.         rts
  147.  
  148. errhook        jsr    endfloat        ;Restore 0-page and unhook error trap.
  149.         txa            ;Preserve error code.
  150.         tay
  151. keepstkptr    ldx    #0        ;Modified.
  152.         inx
  153.         inx
  154.         inx
  155.         inx
  156.         txs
  157.         inx
  158. @a        lda    keepstk,x
  159.         sta    $100,x
  160.         inx
  161.         bne    @a
  162.         ldx    endUnBin1+1
  163.         tya            ;Move floating-point error into acc.
  164.         sec            ;Indicate floating-point error occured.
  165.         rts
  166.  
  167.         endp
  168.  
  169. ******************
  170.  
  171.         export    i2f
  172. i2f        proc
  173.         jsr    startUnary1
  174.         lda    intspace+1,x    ;Get variable int value.
  175.         ldy    intspace,x
  176.         jsr    GIVAYF        ;Convert a,y int into float (in FAC).
  177.         jmp    endUnBin
  178.         endp
  179.  
  180. ***
  181.  
  182.         export    f2i
  183. f2i        proc
  184.         jsr    startUnary
  185.         jsr    QINT
  186.         ldx    endUnBin1+1    ;Restore xreg.
  187.         lda    FACMO
  188.         sta    intspace+1,x
  189.         tay
  190.         lda    FACLO
  191.         sta    intspace,x
  192.         jmp    endUnBin0
  193.         endp
  194.  
  195. ***
  196.  
  197.         export    i2fsetconl
  198. i2fsetconl    proc
  199.         export    i2fsetcon
  200.         import    setcon
  201.         ldy    #0
  202. i2fsetcon    jsr    setcon
  203.         jmp    i2f
  204.         endp
  205.  
  206. ***
  207.  
  208.         export    fout
  209. fout        proc
  210.         export    fout0
  211.         import    rtcout
  212.         jsr    startUnary0
  213. fout0        jsr    endfloat0
  214.         jsr    FOUT
  215.         ldx    #0
  216. @a        lda    FBUFFR,x
  217.         beq    @b
  218.         jsr    rtcout
  219.         inx
  220.         bne    @a        ;Always.
  221. @b        jmp    endUnBin0
  222.         endp
  223.  
  224. ***
  225.  
  226.         export    fvout
  227. fvout        proc
  228.         jsr    startUnary
  229.         jmp    fout0
  230.         endp
  231.  
  232. ***
  233.  
  234.         export    frtsout
  235. frtsout        proc
  236.         pla
  237.         clc
  238.         adc    #5
  239.         sta    @a+1
  240.         pla
  241.         adc    #0
  242.         tay
  243.         pha
  244. @a        lda    #0        ;Modified.
  245.         pha
  246.         sbc    #3        ;-4, since carry is clear.
  247.         bcs    @b
  248.         dey
  249. @b        jmp    fout
  250.         endp
  251.  
  252. ***
  253.  
  254.         export    fmulvar
  255. fmulvar        proc
  256.         jsr    startBinary
  257.         jsr    FMULT        ;Do the multiply.
  258.         jmp    endUnBin
  259.         endp
  260.  
  261. ***
  262.  
  263.         export    fmulcon
  264. fmulcon        proc
  265.         jsr    startBinary0
  266.         jsr    FMULT        ;Do the multiply.
  267.         jmp    endUnBin
  268.         endp
  269.  
  270. ***
  271.  
  272.         export    frtsmul
  273. frtsmul        proc
  274.         pla
  275.         clc
  276.         adc    #5
  277.         sta    @a+1
  278.         pla
  279.         adc    #0
  280.         tay
  281.         pha
  282. @a        lda    #0        ;Modified.
  283.         pha
  284.         sbc    #3        ;-4, since carry is clear.
  285.         bcs    @b
  286.         dey
  287. @b        jmp    fmulcon
  288.         endp
  289.  
  290. ***
  291.  
  292.         export    fdivvar
  293. fdivvar        proc
  294.         jsr    startBinary
  295.         jsr    FDIV        ;Do the divide.
  296.         jmp    endUnBin
  297.         endp
  298.  
  299. ***
  300.  
  301.         export    fdivcon
  302. fdivcon        proc
  303.         jsr    startBinary0
  304.         jsr    FDIV        ;Do the divide.
  305.         jmp    endUnBin
  306.         endp
  307.  
  308. ***
  309.  
  310.         export    frtsdiv
  311. frtsdiv        proc
  312.         pla
  313.         clc
  314.         adc    #5
  315.         sta    @a+1
  316.         pla
  317.         adc    #0
  318.         tay
  319.         pha
  320. @a        lda    #0        ;Modified.
  321.         pha
  322.         sbc    #3        ;-4, since carry is clear.
  323.         bcs    @b
  324.         dey
  325. @b        jmp    fdivcon
  326.         endp
  327.  
  328. ***
  329.  
  330.         export    faddvar
  331. faddvar        proc
  332.         jsr    startBinary
  333.         jsr    FADD        ;Do the add.
  334.         jmp    endUnBin
  335.         endp
  336.  
  337. ***
  338.  
  339.         export    faddcon
  340. faddcon        proc
  341.         jsr    startBinary0
  342.         jsr    FADD        ;Do the add.
  343.         jmp    endUnBin
  344.         endp
  345.  
  346. ***
  347.  
  348.         export    frtsadd
  349. frtsadd        proc
  350.         pla
  351.         clc
  352.         adc    #5
  353.         sta    @a+1
  354.         pla
  355.         adc    #0
  356.         tay
  357.         pha
  358. @a        lda    #0        ;Modified.
  359.         pha
  360.         sbc    #3        ;-4, since carry is clear.
  361.         bcs    @b
  362.         dey
  363. @b        jmp    faddcon
  364.         endp
  365.  
  366. ***
  367.  
  368.         export    fsubvar
  369. fsubvar        proc
  370.         jsr    startBinary
  371.         jsr    FSUB        ;Do the subtract.
  372.         jmp    endUnBin
  373.         endp
  374.  
  375. ***
  376.  
  377.         export    fsubcon
  378. fsubcon        proc
  379.         jsr    startBinary0
  380.         jsr    FSUB        ;Do the subtract.
  381.         jmp    endUnBin
  382.         endp
  383.  
  384. ***
  385.  
  386.         export    frtssub
  387. frtssub        proc
  388.         pla
  389.         clc
  390.         adc    #5
  391.         sta    @a+1
  392.         pla
  393.         adc    #0
  394.         tay
  395.         pha
  396. @a        lda    #0        ;Modified.
  397.         pha
  398.         sbc    #3        ;-4, since carry is clear.
  399.         bcs    @b
  400.         dey
  401. @b        jmp    fsubcon
  402.         endp
  403.  
  404. ***
  405.  
  406.         export    fv2v
  407. fv2v        proc
  408.         jsr    startBinary
  409.         jsr    CONUPK        ;Move value into ARG.
  410.         lda    FACEXP        ;Get A and Z correct.
  411.         jsr    FPWRT        ;Do the exponentation.
  412.         jmp    endUnBin
  413.         endp
  414.  
  415.  
  416.  
  417. ***
  418.  
  419.         export    fv2con
  420. fv2con        proc
  421.         jsr    startBinary0
  422.         jsr    CONUPK        ;Move value into ARG.
  423.         lda    FACEXP        ;Get A and Z correct.
  424.         jsr    FPWRT        ;Do the exponentation.
  425.         jmp    endUnBin
  426.         endp
  427.  
  428. ***
  429.  
  430.         export    frtsv2con
  431. frtsv2con    proc
  432.         pla
  433.         clc
  434.         adc    #5
  435.         sta    @a+1
  436.         pla
  437.         adc    #0
  438.         tay
  439.         pha
  440. @a        lda    #0        ;Modified.
  441.         pha
  442.         sbc    #3        ;-4, since carry is clear.
  443.         bcs    @b
  444.         dey
  445. @b        jmp    fv2con
  446.         endp
  447.  
  448. ***
  449.  
  450.         export    fsgn
  451. fsgn        proc
  452.         jsr    startUnary
  453.         jsr    SGN        ;Get the sign of FAC.
  454.         jmp    endUnBin
  455.         endp
  456.  
  457. ***
  458.  
  459.         export    fabs
  460. fabs        proc
  461.         jsr    startUnary
  462.         jsr    ABS        ;Absolute value of FAC.
  463.         jmp    endUnBin
  464.         endp
  465.  
  466. ***
  467.  
  468.         export    fint
  469. fint        proc
  470.         jsr    startUnary
  471.         jsr    INT        ;Greatest integer value of FAC.
  472.         jmp    endUnBin
  473.         endp
  474.  
  475. ***
  476.  
  477.         export    fsqr
  478. fsqr        proc
  479.         jsr    startUnary
  480.         jsr    SQR        ;Take square root of FAC.
  481.         jmp    endUnBin
  482.         endp
  483.  
  484. ***
  485.  
  486.         export    flog
  487. flog        proc
  488.         jsr    startUnary
  489.         jsr    LOG        ;Log base e of FAC.
  490.         jmp    endUnBin
  491.         endp
  492.  
  493. ***
  494.  
  495.         export    fexp
  496. fexp        proc
  497.         jsr    startUnary
  498.         jsr    EXP        ;Raise e to FAC power.
  499.         jmp    endUnBin
  500.         endp
  501.  
  502. ***
  503.  
  504.         export    frnd
  505. frnd        proc
  506.         jsr    startUnary
  507.         jsr    CONUPK        ;Move value into ARG.
  508.         jsr    @swaplastrnd    ;Swap last 'random' number into 0-page.
  509.         jsr    RND        ;Forms a 'random' number in FAC.
  510.         jsr    @swaplastrnd
  511.         jmp    endUnBin
  512. @swaplastrnd    ldx    #4
  513. @a        lda    $C9,x
  514.         ldy    @lastrnd,x
  515.         sty    $C9,x
  516.         sta    @lastrnd,x
  517.         dex
  518.         bpl    @a
  519.         rts
  520. @lastrnd        dc.b    128,79,199,82,89    ;This is the AppleSoft initial 'random' number.
  521.  
  522.         endp
  523.  
  524. ***
  525.  
  526.         export    fcos
  527. fcos        proc
  528.         jsr    startUnary
  529.         jsr    COS        ;COS(FAC).
  530.         jmp    endUnBin
  531.         endp
  532.  
  533. ***
  534.  
  535.         export    fsin
  536. fsin        proc
  537.         jsr    startUnary
  538.         jsr    SIN        ;SIN(FAC).
  539.         jmp    endUnBin
  540.         endp
  541.  
  542. ***
  543.  
  544.         export    ftan
  545. ftan        proc
  546.         jsr    startUnary
  547.         jsr    TAN        ;TAN(FAC).
  548.         jmp    endUnBin
  549.         endp
  550.  
  551. ***
  552.  
  553.         export    fatn
  554. fatn        proc
  555.         jsr    startUnary
  556.         jsr    ATN        ;ARCTAN(FAC).
  557.         jmp    endUnBin
  558.         endp
  559.  
  560. ***
  561.  
  562.         export    i2fsetvars
  563. i2fsetvars    proc
  564.         pla
  565.         sta    @gv+1
  566.         pla
  567.         sta    @gv+2
  568.         txa
  569.         pha
  570. @loop        jsr    @getval
  571.         cmp    #255
  572.         beq    @exit
  573.         tax
  574.         jsr    @getval
  575.         sta    floatspace,x
  576.         jsr    @getval
  577.         sta    floatspace+1,x
  578.         jsr    i2f
  579.         jmp    @loop
  580. @exit        pla
  581.         tax
  582.         lda    @gv+2
  583.         pha
  584.         lda    @gv+1
  585.         pha
  586.         rts
  587. @getval        inc    @gv+1
  588.         bne    @gv
  589.         inc    @gv+2
  590. @gv        lda    $2000        ;Address modified.
  591.         rts
  592.         endp
  593.  
  594. ***
  595.  
  596.         export    frtssetcon
  597. frtssetcon    proc
  598.         pla
  599.         sta    @gv+1
  600.         pla
  601.         sta    @gv+2
  602.         txa
  603.         pha
  604.         ldy    #5
  605. @loop        inc    @gv+1
  606.         bne    @gv
  607.         inc    @gv+2
  608. @gv        lda    $2000        ;Address modified.
  609.         sta    floatspace,x
  610.         inx
  611.         dey
  612.         bne    @loop
  613.         pla
  614.         tax
  615.         lda    @gv+2
  616.         pha
  617.         lda    @gv+1
  618.         pha
  619.         rts
  620.         endp
  621.  
  622. ***
  623.  
  624.         export    fsetcon
  625. fsetcon        proc
  626.         sta    @gv+1
  627.         sty    @gv+2
  628.         txa
  629.         pha
  630.         ldy    #0
  631. @gv        lda    $2000,y        ;Address modified.
  632.         sta    floatspace,x
  633.         inx
  634.         iny
  635.         cpy    #5
  636.         bcc    @gv
  637.         pla
  638.         tax
  639.         rts
  640.         endp
  641.  
  642. ***
  643.  
  644.         export    fsetzero
  645. fsetzero        proc
  646.         lda    #0
  647.         sta    floatspace,x
  648.         sta    floatspace+1,x
  649.         sta    floatspace+2,x
  650.         sta    floatspace+3,x
  651.         sta    floatspace+4,x
  652.         rts
  653.         endp
  654.  
  655. ***
  656.  
  657.         export    fsetvars
  658. fsetvars        proc
  659.         pla
  660.         sta    @gv+1
  661.         pla
  662.         sta    @gv+2
  663.         txa
  664.         pha
  665. @loop        jsr    @getval
  666.         cmp    #255
  667.         beq    @exit
  668.         tax
  669.         ldy    #5
  670. @a        jsr    @getval
  671.         sta    floatspace,x
  672.         inx
  673.         dey
  674.         bne    @a
  675.         beq    @loop        ;Always.
  676. @exit        pla
  677.         tax
  678.         lda    @gv+2
  679.         pha
  680.         lda    @gv+1
  681.         pha
  682.         rts
  683. @getval        inc    @gv+1
  684.         bne    @gv
  685.         inc    @gv+2
  686. @gv        lda    $2000        ;Address modified.
  687.         rts
  688.         endp
  689.  
  690. ***
  691.  
  692.         export    fvcmp
  693. fvcmp        proc
  694.         jsr    startBinary
  695.         jsr    FCOMP        ;Do the compare.
  696.         ldx    endUnBin0+1
  697.         eor    #1
  698.         cmp    #1
  699.         rts
  700.         endp
  701.  
  702. ***
  703.  
  704.         export    fcmp
  705. fcmp        proc
  706.         jsr    startBinary0
  707.         jsr    FCOMP        ;Do the compare.
  708.         ldx    endUnBin0+1
  709.         eor    #1
  710.         cmp    #1
  711.         rts
  712.         endp
  713.  
  714. ***
  715.  
  716.         export    frtscmp
  717. frtscmp        proc
  718.         pla
  719.         clc
  720.         adc    #5
  721.         sta    @a+1
  722.         pla
  723.         adc    #0
  724.         tay
  725.         pha
  726. @a        lda    #0        ;Modified.
  727.         pha
  728.         sbc    #3        ;-4, since carry is clear.
  729.         bcs    @b
  730.         dey
  731. @b        jmp    fcmp
  732.         endp
  733.  
  734. ***
  735.  
  736.         export    readfloat
  737. readfloat    proc
  738.         import    getdatabyte
  739.         txa
  740.         pha
  741.         ldy    #5
  742. @a        jsr    getdatabyte
  743.         sta    floatspace,x
  744.         inx
  745.         dey
  746.         bne    @a
  747.         pla
  748.         tax
  749.         rts
  750.         endp
  751.  
  752. ***
  753.  
  754.         export    fvarcpy
  755. fvarcpy        proc
  756.         txa
  757.         pha
  758.         lda    #5
  759.         sec
  760. @a        pha
  761.         lda    floatspace,y
  762.         iny
  763.         sta    floatspace,x
  764.         inx
  765.         pla
  766.         sbc    #1
  767.         bne    @a
  768.         pla
  769.         tax
  770.         rts
  771.         endp
  772.  
  773. ***
  774.  
  775. * Step 1:  Initialize stuff.
  776. * Step 2:  Parse the string and get the values of the components.
  777. * Step 3:  Use the component values to generate a 5-byte float number.
  778. * Step 4:  Put a pointer to this value in a,y and return.
  779.  
  780.         export    fstrval
  781. fstrval        proc
  782.         export    fmidstrval
  783.         import    strinfo, strlen, currentstr, nextchr
  784.  
  785.         ldy    #0        ;Step 1:  Initialize stuff.
  786. fmidstrval    jsr    strinfo
  787.         sta    getchr+1
  788.         stx    getchr+2        ;Stuff initialized -- step 1 done.
  789.         lda    #0        ;Initialize values.
  790.         ldx    #sgn-exp        ;Initialize work values.
  791. @init        sta    exp,x
  792.         dex
  793.         bpl    @init
  794.         sec            ;Set sgn to $80, instead of 0.
  795.         ror    sgn        ;Assume positive.
  796.         lda    #$80+32        ;Initialize exponent.
  797.         sta    exp        ;Done with step 1.
  798.  
  799.                     ;Step 2:  Parse string and get values.
  800.         jsr    getchr        ;Get first character (good place to start).
  801.         bcs    @b        ;It is a digit, so go handle it.
  802.         cmp    #'-'
  803.         bne    @a
  804.         asl    sgn        ;It is negative after all.
  805.         bcs    @nextchr        ;Always.
  806. @a        cmp    #'+'
  807.         bne    @d        ;It isn't a digit or a plus.
  808.                     ;Ignore optional +.
  809.  
  810. @nextchr        jsr    getchr        ;Get next character.
  811.         bcc    @d        ;It isn't a digit.
  812.  
  813. @b        pha
  814.         jsr    mant10        ;Multiply mantissa by 10.
  815.         pla
  816.         and    #$0F
  817.         clc
  818.         adc    mant0
  819.         sta    mant0
  820.         bcc    @c
  821.         inc    mant1
  822.         bne    @c
  823.         inc    mant2
  824.         bne    @c
  825.         inc    mant3
  826.         bne    @c
  827.         inc    extmant
  828. @c        jsr    revnorm        ;Take care of mantissa overflow, if any.
  829.  
  830.         lda    dptflg
  831.         beq    @nextchr        ;Haven't hit decimal-point yet.
  832.         inc    dptcnt        ;Passed decimal-point, so count decimal digit.
  833.         bne    @nextchr        ;Always.
  834.  
  835. @d        cmp    #'.'
  836.         bne    @e        ;Not a decimal-point either.
  837.         lda    dptflg
  838.         bne    @m        ;Already had a decimal-point, so this one means stop.
  839.         inc    dptflg        ;Flag that we hit the decimal-point.
  840.         bne    @nextchr        ;Go do more characters.
  841.  
  842. @e        ora    #$20        ;See if we have an exponent part.
  843.         cmp    #'e'
  844.         bne    @m        ;No exponent part.
  845.  
  846. @f        jsr    getchr        ;Get the value of the exponent part.
  847.         bcs    @h        ;It is a digit, so go handle it.
  848.         cmp    #'-'        ;See if the exponent is negative.
  849.         bne    @g
  850.         ror    expneg        ;Set bit 7, since it is negative.
  851.         bcc    @nextexpchr    ;Always.
  852. @g        cmp    #'+'
  853.         bne    @m        ;It isn't a digit or a plus.
  854.                     ;Ignore optional +.
  855.  
  856. @nextexpchr    jsr    getchr
  857.         bcc    @m        ;It isn't a digit.
  858. @h        and    #$0F
  859.         sta    @expdigit+1
  860.         lda    expval
  861.         asl    a        ;*2
  862.         asl    a        ;*4
  863.         adc    expval        ;*5
  864.         asl    a        ;*10
  865. @expdigit    adc    #0        ;Modified.        
  866.         sta    expval
  867.         jmp    @nextexpchr    ;Get next exponent character.
  868.                     ;Done with step 2.
  869.  
  870.  
  871.                     ;Step 3:  Use the component values to generate
  872.                     ;a 5-byte float number.
  873. @m        sty    nextchr        ;Pass back info where we stopped in the string.
  874.         lda    mant0        ;Check for special case of 0.
  875.         ora    mant1
  876.         ora    mant2
  877.         ora    mant3
  878.         bne    @mm        ;It is not 0.
  879.         sta    exp
  880.         beq    exit        ;It is 0, so we are done.
  881.  
  882. @mm        jsr    normalize    ;Normalize the number.
  883.  
  884.         lda    expval        ;Adjust the exponent value.
  885.         asl    expneg
  886.         bcc    @n        ;Exponent positive.
  887.         eor    #$FF        ;Change sign of exponent value.
  888.         adc    #0        ;Carry set, so we add 1 here.
  889. @n        sec
  890.         sbc    dptcnt        ;Adjust for number of decimal digits.
  891.         sta    expval        ;This is the real exponent value.
  892.  
  893.         beq    dosgn        ;No exponent adjustment necessary.
  894.         bpl    expmul        ;For this case, we multiply to adjust for exponent.
  895.  
  896. expdiv        ldy    #0        ;Divide the mantissa by 10.
  897.         sty    extmant        ;Use extended mantissa for divide precision.
  898.         ldx    #40        ;Mantissa is 40 bits.
  899. @a        tya            ;Remainder will be in the y when we are done.
  900.         asl    extmant
  901.         rol    mant0
  902.         rol    mant1
  903.         rol    mant2
  904.         rol    mant3
  905.         rol    a
  906.         tay
  907.         sec
  908.         sbc    #10
  909.         bcc    @b        ;This factor of 10 didn't go into it.
  910.         tay            ;It did go into it, so record that it did.
  911.         inc    extmant
  912. @b        dex
  913.         bne    @a        ;More bits to try.
  914.  
  915.         jsr    extnormalize    ;Normalize extended mantissa.
  916.  
  917.         inc    expval        ;See if we have done enough exponent adjustment.
  918.         bne    expdiv        ;More adjusting to go.
  919.  
  920. dosgn        lda    mant3
  921.         eor    sgn
  922.         sta    mant3        ;Done with step 3.
  923.  
  924. exit        lda    #<exp        ;Step 4:  Return pointer to float value.
  925.         ldy    #>exp
  926.         ldx    currentstr
  927.         rts
  928.  
  929. expmul        jsr    mant10        ;Multiply mantissa by 10.
  930.         jsr    revnorm        ;Take care of mantissa overflow.
  931.         dec    expval        ;See if we have done enough exponent adjustment.
  932.         bne    expmul        ;More adjusting to go.
  933.         beq    dosgn        ;We be done.
  934.  
  935. revnorm        lda    extmant
  936.         beq    @rts
  937. @a        lsr    a        ;Reverse normalize mantissa.
  938.         ror    mant3
  939.         ror    mant2
  940.         ror    mant1
  941.         ror    mant0
  942.         inc    exp
  943.         tax
  944.         bne    @a        ;More normalizing to do.
  945.         bcs    incmant        ;Round the mantissa.
  946. @rts        rts
  947.  
  948. ***
  949.  
  950. normalize    lda    #0
  951.         sta    extmant
  952. extnormalize    ldy    mant3
  953.         bmi    rts0        ;Mantissa is normalized to start with.
  954. @a        dec    exp
  955.         asl    extmant
  956.         rol    mant0
  957.         rol    mant1
  958.         rol    mant2
  959.         rol    mant3
  960.         bpl    @a        ;Not normalized yet.
  961.         lda    extmant        ;Round the mantissa, if necessary.
  962.         bpl    rts0        ;Not necessary.
  963. incmant        inc    mant0
  964.         bne    rts0
  965.         inc    mant1
  966.         bne    rts0
  967.         inc    mant2
  968.         bne    rts0
  969.         inc    mant3
  970.         bne    rts0
  971.         lda    #$FF
  972.         sta    mant0
  973.         sta    mant1
  974.         sta    mant2
  975.         sta    mant3
  976. rts0        rts
  977.  
  978. mant10        lda    #0        ;extmant is for overflow extension.
  979.         sta    extmant
  980.  
  981.         ldx    #-4        ;Push mantissa on stack, hi-byte first.
  982. @a        lda    mant3+4-256,x
  983.         pha
  984.         inx
  985.         bne    @a
  986.  
  987.         jsr    @times2        ;*2
  988.         jsr    @times2        ;*4
  989.  
  990.         ldx    #3        ;*5
  991. @b        pla
  992.         adc    mant3,x
  993.         sta    mant3,x
  994.         dex
  995.         bpl    @b
  996.         bcc    @times2
  997.         inc    extmant
  998.  
  999. @times2        asl    mant0        ;Final time here, *10.
  1000.         rol    mant1
  1001.         rol    mant2
  1002.         rol    mant3
  1003.         rol    extmant
  1004.         rts            ;Carry clear on exit.
  1005.  
  1006.  
  1007. getchr        lda    $2000,y        ;Modified.
  1008.         cpy    strlen
  1009.         bcs    @eos        ;We have reached the end of the string.
  1010.         iny
  1011.  
  1012.         cmp    #'9'+1
  1013.         bcs    @clc        ;Not a digit.
  1014.         cmp    #'0'        ;Sets carry if it is a digit.
  1015.         rts
  1016.  
  1017. @eos        lda    #0
  1018. @clc        clc
  1019. @rts        rts
  1020.  
  1021. exp        dc.b    0
  1022. mant3        dc.b    0
  1023. mant2        dc.b    0
  1024. mant1        dc.b    0
  1025. mant0        dc.b    0
  1026. extmant        dc.b    0
  1027. dptflg        dc.b    0
  1028. dptcnt        dc.b    0
  1029. expval        dc.b    0
  1030. expneg        dc.b    0
  1031. sgn        dc.b    0
  1032.  
  1033.         endp
  1034.  
  1035. ***
  1036.  
  1037.         END
  1038.